var stepMessage = function(obj, text, css){
	//remove class image
	$.map(obj, function(n){
		$("span[id='" + n + "']").removeClass('message').removeClass('error');
	});
	
	//remove corresponding image
	$.map(obj, function(n){
		$("img[id='" + n + "']").attr('src', '../images/blank.jpg');			
	});
	
	$.map(obj, function(n){
		$("span[id='" + n + "']").html(text).addClass(css);			
	});	
	return false;
}


var imageLoader = function(obj){
	//remove corresponding span message
	$.map(obj, function(n){
		$("span[id='" + n + "']").html('');			
	});
	
	$.map(obj, function(n){
		$("img[id='" + n + "']").attr('src', "../images/rotator.gif");
	});
	return false;
}


function creationComplete(id, adid, hrid, rid){		
	var jsonData = "";
	
	var api = '../controller/CreationHandler.cfm?api=CompleteCreation';
	
	var postData = {
		StepID: $("#StepID").val()
	};
	
	//attemps to create AD account
	$.ajax({
		type:'POST', url:api, dataType: 'json', async: true, data: postData,
		success: function(json) { jsonData = json },
		complete: function(){	
			if( jsonData['STATUS'] == 'Fail' )
			{
				//stepMessage( ['Notice'], jsonData['MESSAGE'], 'error');
                $("#Notice").html("Error during step completion.");
				return false;
			}
			else if( jsonData['STATUS'] == 'Completed' )
			{
				stepMessage( ['Notice'], "Employee account creation completed.  <a href='../status.cfm'>Click here</a> to go to Status.", 'message');
				return false;
			}
			else{return false;}
		},
		error: function(){ return false; }
	});	
}




function rosetta(){		
	var jsonData = "";
	
	var api = '../controller/CreationHandler.cfm?api=NonSCFMasterCreation';
	
	var postData = {
		StepID: $("#StepID").val()
	};
	
	imageLoader( ['Rosetta'] );
	
	//attemps to create AD account
	$.ajax({
		type:'POST', url:api, dataType: 'json', async: true, data: postData,
		success: function(json) { jsonData = json },
		complete: function(){	
			if( jsonData['STATUS'] == 'Fail' )
			{
				stepMessage( ['Rosetta'], jsonData['MESSAGE'], 'error');
				return false;
			}
			else if( jsonData['STATUS'] == 'Created' )
			{
				stepMessage( ['Rosetta'], jsonData['MESSAGE'], 'message');
				creationComplete( );
				return false;
			}
			else{return false;}
		},
		error: function(){ return false; }
	});	
}



function adCreation(){
	var jsonData = "";
	
	var api = '../controller/CreationHandler.cfm?api=ADCreation';
	
	imageLoader( ['AD'] );
	
	//attemps to create AD account
	$.ajax({
		type:'POST', url:api, dataType: 'json', async: true, data: {StepID: $("#StepID").val()},
		success: function(json) { jsonData = json },
		complete: function(){
			if( jsonData['STATUS'] == 'Fail' )
			{
				stepMessage( ['AD'], jsonData['MESSAGE'], 'error');
				return false;
			}
			else if(jsonData['STATUS'] == 'Created')
			{
				stepMessage( ['AD'], jsonData['MESSAGE'], 'message');
				rosetta( );
				return false;
			}
			else{return false;}
		},
		error: function(){ return false; }
	});	
}


function bindElements(){
	//start creation process.  This function will call other functions when each request is complete.
	adCreation();	
}


$(document).ready(function(){
	//reset step messages before creation
	stepMessage( ['AD', 'HRProd', 'Rosetta'], 'Waiting...', 'error' );
	setTimeout('bindElements()', 1000);

});